//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "EURCurrency.h"
using namespace Cephei::QL::Currencies;
#include <gen/QL/Math/Rounding.h>
#include <gen/QL/Currency.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Currencies::CEURCurrency::CEURCurrency () : CCurrency(CEURCurrency::typeid)
{
    try
    {
#ifdef HANDLE
        _phEURCurrency = NULL;
#endif
        _ppEURCurrency = new boost::shared_ptr<QuantLib::EURCurrency> (new QuantLib::EURCurrency ( ));
        SetCurrency (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppEURCurrency));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Currencies::CEURCurrency::CEURCurrency (boost::shared_ptr<QuantLib::EURCurrency>& childNative, Object^ owner) : CCurrency(CEURCurrency::typeid)
{
#ifdef HANDLE
	_phEURCurrency = NULL;
#endif
	_ppEURCurrency = &childNative;
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppEURCurrency));
}
Cephei::QL::Currencies::CEURCurrency::CEURCurrency (QuantLib::EURCurrency& childNative, Object^ owner) : CCurrency(CEURCurrency::typeid)
{
#ifdef HANDLE
	_phEURCurrency = NULL;
#endif
	_ppEURCurrency = new boost::shared_ptr<QuantLib::EURCurrency> (&childNative);
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppEURCurrency));
    _EURCurrencyOwner = owner;
    _CurrencyOwner = owner;
}

Cephei::QL::Currencies::CEURCurrency::CEURCurrency (CEURCurrency^ copy) : CCurrency(CEURCurrency::typeid)
{
#ifdef HANDLE
	_phEURCurrency = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEURCurrency = new boost::shared_ptr<QuantLib::EURCurrency> (copy->GetShared());
        _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppEURCurrency));
    }
}
Cephei::QL::Currencies::CEURCurrency::CEURCurrency (PLATFORM::Type^ t) : CCurrency(CEURCurrency::typeid)
{
#ifdef HANDLE
	_phEURCurrency = NULL;
#endif
	if (!t->IsSubclassOf(CEURCurrency::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Currencies::CEURCurrency::CEURCurrency (QuantLib::Handle<QuantLib::EURCurrency>& childNative, Object^ owner)  : CCurrency(CEURCurrency::typeid)
{
	_phEURCurrency = &childNative;
	_ppEURCurrency = &static_cast<boost::shared_ptr<QuantLib::EURCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppEURCurrency));
    _EURCurrencyOwner = owner;
}
Cephei::QL::Currencies::CEURCurrency::CEURCurrency (QuantLib::Handle<QuantLib::EURCurrency> childNative)  : CCurrency(CEURCurrency::typeid)
{
	_phEURCurrency = &childNative;
	_ppEURCurrency = &static_cast<boost::shared_ptr<QuantLib::EURCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppEURCurrency));
}
#endif
#ifdef STRUCT
Cephei::QL::Currencies::CEURCurrency::CEURCurrency (QuantLib::EURCurrency childNative)  : CCurrency(CEURCurrency::typeid)
{
#ifdef HANDLE
	_phEURCurrency = NULL;
#endif
	_ppEURCurrency = new boost::shared_ptr<QuantLib::EURCurrency> (new QuantLib::EURCurrency (childNative));
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppEURCurrency));
}
#endif

Cephei::QL::Currencies::CEURCurrency::~CEURCurrency ()
{
    if (_ppEURCurrency != NULL)
    {
	    delete _ppEURCurrency;
        _ppEURCurrency = NULL;
    }
}
Cephei::QL::Currencies::CEURCurrency::!CEURCurrency ()
{
    if (_ppEURCurrency != NULL)
    {
	    delete _ppEURCurrency;
    }
}
QuantLib::EURCurrency& Cephei::QL::Currencies::CEURCurrency::GetReference ()
{
    if (_ppEURCurrency == NULL) throw REFNEW NativeNullException ();
	return **_ppEURCurrency;
}
boost::shared_ptr<QuantLib::EURCurrency>& Cephei::QL::Currencies::CEURCurrency::GetShared ()
{
    if (_ppEURCurrency == NULL) throw REFNEW NativeNullException ();
	return *_ppEURCurrency;
}
QuantLib::EURCurrency* Cephei::QL::Currencies::CEURCurrency::GetPointer ()
{
    if (_ppEURCurrency == NULL) throw REFNEW NativeNullException ();
	return &**_ppEURCurrency;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EURCurrency>& Cephei::QL::Currencies::CEURCurrency::GetHandle ()
{
	if (_phEURCurrency == NULL)
	{
		_phEURCurrency = new Handle<QuantLib::EURCurrency> (*_ppEURCurrency);
	}
	return *_phEURCurrency;
}
#endif
bool Cephei::QL::Currencies::CEURCurrency::HasNative () 
{
	return (_ppEURCurrency != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Currencies::IEURCurrency^ Cephei::QL::Currencies::CEURCurrency_Factory::Create ()
{
    return REFNEW CEURCurrency ();
}
